package com.jingbo.DAO.Record;

import com.jingbo.Dbutil.DBUtil;
import com.jingbo.entity.Profile;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jingbo.DAO.Record
 * @Description:
 * @Date 2022/5/27 14:52
 */
public class pecordimpl implements Record {
    private QueryRunner qr = new QueryRunner(DBUtil.getDateSource());
    /**
     * 添加档案
     * @param profile
     * @return
     */
    @Override
    public int AddPecord(Profile profile) {
        String sql ="insert into profile (name,birthday,gender,career,address,mobile,picture)value(?,?,?,?,?,?,?)";
        try {
            return qr.update(sql,profile.getName(),profile.getBirthday(),profile.getGender(),profile.getCareer(),profile.getAddress(),profile.getMobile(),profile.getPicture());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查看所有
     * @return
     */
    @Override
    public List<Profile> selectPecord() {
        String sql="select * from profile";
        try {
            return qr.query(sql,new BeanListHandler<Profile>(Profile.class));
        } catch (SQLException e) {
        }
        return null;
    }

    @Override
    public int update(Profile p, Integer id) {
        String sql ="update  profile set  name=?, birthday=?, gender=?,career=?,address=?,mobile=?,picture=?  where id=?";
        try {
            int update = qr.update(sql, p.getName(), p.getBirthday(), p.getGender(), p.getCareer(), p.getAddress(), p.getMobile(), p.getPicture(),id);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除档案信息
     * @param id
     */
    @Override
    public int delete(Integer id) {
        String sql ="delete from profile where id=?";
        try {
            int update = qr.update(sql, id);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public Profile allone(Integer id) {
        String sql ="select * from profile  where id=? ";
        try {
            Profile query = qr.query(sql, new BeanHandler<Profile>(Profile.class), id);
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer countpaging() {
        String sql =" select COUNT(*)  from profile";
        try {
            long query = (long) qr.query(sql, new ScalarHandler());
            return (int)query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分页查询
     * @param begin
     * @param finish
     * @return
     */
    @Override
    public List<Profile> all(Integer begin, Integer finish) {
        String sql ="select * from  profile  LIMIT ? , ?";
        try {
            return qr.query(sql, new BeanListHandler<Profile>(Profile.class),begin,finish);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
